package com.loan.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.loan.enums.S;
import com.loan.model.Data;
import com.loan.model.Loan;
import com.loan.model.Repay;
import com.loan.model.Slider;
import com.loan.model.User;
import com.loan.model.Version;
import com.loan.service.SliderService;
import com.loan.service.TokenService;
import com.loan.service.VersionService;
import com.loan.util.AES;
import com.loan.util.DateUtil;
import com.loan.util.Json;
import com.loan.util.Money;
import com.loan.util.Str;

/**
 *  首页数据
 * @since 2018-03-03
 * @author 无处不在的二叔
 */ 
public class AppController extends Controller {
	private static SliderService slider = new SliderService();
	private static TokenService token = new TokenService();
	/**
	 * 首页数据控制器
	 */
	@Clear
	public void index() { 
		getResponse().setHeader("Access-Control-Allow-Origin","*"); 
		String type = getPara("type","app");
		if(Str.eq("app", type)) {

			// 总注册人数
			List<Slider> sl = slider.findEnable();
			List<Loan> u = Loan.dao.find("select * from loan where status<>? and cid=? order by create_time desc limit 0,5",S.VERIFY,getPara("cid","1"));
 
			renderJson(Json.ok("数据获取成功！",Data.put("sliders",sl,"logs",u)));
			
		}else if(Str.eq("admin", type)) {
			
			User u1 = token.findUserByToken(getPara("token",""));
			Map<String,Long> map = new HashMap<String,Long>();
			String startTime = getPara("startTime");
			String endTime = getPara("endTime");
			String startTime2 = "";
			String endTime2 = "";
			String dateWhere = "";
			if(Str.nb(startTime)) {
				dateWhere += " and {0}>=''"+startTime+"''";
				startTime2 = DateUtil.adjust(DateUtil.parse(getPara("startTime")), -1, DateUtil.DATETIME_DEFAULT_FORMAT);
			}
			if(Str.nb(endTime)) {
				dateWhere += " and {0}<=''"+endTime+"''";
				endTime2 = DateUtil.adjust(DateUtil.parse(getPara("endTime")), -1, DateUtil.DATETIME_DEFAULT_FORMAT);
			}

			int cid = u1.getCid();
			String cidStr = cid>1 ? " 1=1 and cid="+cid : " 1=1  ";
			String cidStr2 = cid>1 ? " 1=1 and u.cid="+cid : " 1=1  ";

			/**申请数据*/
			
			// 待初审申请
			Loan ln = Loan.dao.findFirst("select count(l.id) count from loan l left join user u on l.user_id=u.id where "+cidStr2+Str.format(dateWhere, "l.create_time")+" and l.status='审核中'  and u.checks='初审审核中'");
			map.put("a1", Money.getLong(ln.getLong("count")));
			
			// 待再审申请
			ln = Loan.dao.findFirst("select count(l.id) count from loan l left join user u on l.user_id=u.id where "+cidStr2+Str.format(dateWhere, "l.create_time")+" and l.status='审核中'  and u.checks='复审审核中'");
			map.put("a2", Money.getLong(ln.getLong("count")));
			
			// 通过申请
			ln = Loan.dao.findFirst("select count(id) as count from loan where "+cidStr+Str.format(dateWhere, "check_time"));
			map.put("a3", Money.getLong(ln.getLong("count")));

			// 拒绝申请总数
			User u = User.dao.findFirst("select count(id) as count from user where "+cidStr+Str.format(dateWhere, "create_time")+" and (checks=? or checks=?)",S.FCHECK_REJECT,S.SCHECK_REJECT);
			map.put("a4", Money.getLong(u.getLong("count")));
			
			// 通过续期申请
			Repay r = Repay.dao.findFirst("select count(id) as count from repay where "+cidStr+Str.format(dateWhere, "divide_time")+" and is_divide=1");
			map.put("a5", Money.getLong(r.getLong("count")));
			

			/**交易数据*/
			 
			// 已放款金额
			ln = Loan.dao.findFirst("select sum(amount) as sum from loan where "+cidStr+Str.format(dateWhere, "remittance_time")+" and remittance_time is not null");
			map.put("b1", Money.getLong(ln.getLong("sum")));
			 
			// 待放款金额
			ln = Loan.dao.findFirst("select sum(amount) as sum from loan where "+cidStr+Str.format(dateWhere, "check_time")+" and status=?",S.PAYING);
			map.put("b2", Money.getLong(ln.getLong("sum")));
			
			// 应还款金额
			Repay rp = Repay.dao.findFirst("select sum(period_total) as total from repay where "+cidStr+Str.format(dateWhere, "repay_time")+" and (status='还款中' or status='已逾期')");
			map.put("b3", Money.getLong(rp.getLong("total")));
			
			// 实际还款金额 
			rp = Repay.dao.findFirst("select sum(period_total) as total from repay where "+cidStr+Str.format(dateWhere, "real_repay_time")+" and status=? or status=?",S.REPAY_SUCCESS,S.VERIFY);
			map.put("b4", Money.getLong(rp.getLong("total")));
			
			// 逾期已还款金额
			rp = Repay.dao.findFirst("select sum(period_total) as total from repay where "+cidStr+Str.format(dateWhere, "repay_time")+" and status=?",S.REPAY_DELAY);
			map.put("b5", Money.getLong(rp.getLong("total")));

			// 新增逾期金额
			if(Str.nb(startTime2) && Str.nb(endTime2)) {
				rp = Repay.dao.findFirst("select sum(period_total) as total from repay where "+cidStr+Str.format(dateWhere, "repay_time")+" and status=?",S.REPAY_DELAY);
				map.put("b6", Money.getLong(rp.getLong("total")));
			}
			
			// 手动还款金额
			rp = Repay.dao.findFirst("select sum(sect) as total from repay where "+cidStr+Str.format(dateWhere, "sect_time"));
			map.put("b7", Money.getLong(rp.getLong("total")));
			

			/**用户数据*/
			
			// 注册人数
			u = User.dao.findFirst("select count(id) as count from user where "+cidStr+Str.format(dateWhere, "create_time"));
			map.put("c1", Money.getLong(u.getLong("count")));
			
			// 提交申请人数
			u = User.dao.findFirst("select count(l.id) as count from loan l left join user u on l.user_id=u.id where "+cidStr2+Str.format(dateWhere, "l.create_time"));
			map.put("c2", Money.getLong(u.getLong("count")));
			
			// 待还款用户
			u = User.dao.findFirst("select count(u.id) as count from user u left join repay r on r.user_id=u.id where "+cidStr2+Str.format(dateWhere, "r.repay_time")+" and r.status=?",S.REPAY_PROCESS);
			map.put("c3", Money.getLong(u.getLong("count")));
			
			// 已还款用户
			u = User.dao.findFirst("select count(u.id) as count from user u left join repay r on r .user_id=u.id where "+cidStr2+Str.format(dateWhere, "r.real_repay_time")+" and r.status=?",S.REPAY_SUCCESS);
			map.put("c4", Money.getLong(u.getLong("count")));
			
			// 新增逾期用户
			if(Str.nb(startTime2) && Str.nb(endTime2)) {
				u = User.dao.findFirst("select count(u.id) as count from user u left join repay r on r.user_id=u.id where "+cidStr2+" and r.repay_time>=? and r.repay_time<=?  and r.status=?",startTime2,endTime2,S.REPAY_DELAY);
				map.put("c5", Money.getLong(u.getLong("count")));
			}
			
			// 待放款用户
			u = User.dao.findFirst("select count(u.id) as count from user u left join loan l on l.user_id=u.id where "+cidStr2+Str.format(dateWhere, "l.create_time")+" and l.status=?",S.PAYING);
			map.put("c6", Money.getLong(u.getLong("count")));
			
			// 已放款用户
			u = User.dao.findFirst("select count(u.id) as count from user u left join loan l on l.user_id=u.id where "+cidStr2+Str.format(dateWhere, "l.remittance_time")+" and remittance_time is not null");
			map.put("c7", Money.getLong(u.getLong("count")));
			
			renderJson(Json.ok("数据查询成功！",map));
		}
	}
	
	/**
	 * APP版本检查 
	 */
	@Clear 
	public void version() throws Exception {
		String system = getPara("system");
		String type = getPara("type");  
		String code = getPara("code");
		Version version = VersionService.checkVersion(system, type, code);
		if(version!=null && Str.nb(version.getStr("code"))){
			renderJson(Json.ok("检测到新版本！", Data.put("version", version)));
		}else{
			renderJson(Json.error("已是最新版本！"));
		}
	} 
	
	/**
	 * APP版本检查 
	 */
	@Clear 
	public void setVersion() throws Exception {
		String system = getPara("system");
		String type = getPara("type");  
		String code = AES.decode(getPara("code"));
		Db.update("update version set code=? where system=? and type=?",code,system,type);
		renderJson(Json.ok("版本更新成功！"));
	} 
}
 